package ru.myrtle.umf.codegen;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class GenRule<ModelType, ContextType> {

    public interface Selector<ModelType> {

        public Collection<?> getContent(ModelType model);

    }

    /*
     *
     */

    private final String name;
    private final Selector<ModelType> selector;
    private final Collection<GenDomain<ContextType>> domains =
            new ArrayList<GenDomain<ContextType>>();

    /*
     *
     */

    public GenRule(String name, Selector<ModelType> selector, GenDomain<ContextType>... domains) {
        this.name = name;
        this.selector = selector;
        this.domains.addAll(
                Arrays.asList(domains)
        );
    }

    /*
     *
     */

    public void generate(ModelType model, ResourceSet resourceSet, GenMonitor monitor, ContextType context)
            throws GenException {
        
        monitor.ruleStarted(name);

        for (Object object : selector.getContent(model)) {
            monitor.ruleObject(object);

            for (GenDomain<ContextType> domain : domains) {
                domain.generate(
                        object, resourceSet, monitor, context
                );
            }
        }

        monitor.ruleFinished();
    }
}
